package com.peng.order.mqtt;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.peng.order.constant.Constant;
import com.peng.order.dao.repository.OrderRepository;
import com.peng.order.pojo.dto.DeviceResultParam;
import com.peng.order.pojo.po.ChargingBillFailPO;
import com.peng.order.pojo.po.ChargingBillSuccessPO;
import com.peng.order.service.OrderService;
import com.peng.util.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.*;

/**
 * @author： Peng
 * @date： 2024/6/27
 * @version: 1.0
 */

@Slf4j
public class MqttConsumer implements MqttCallbackExtended {
    private OrderRepository orderRepository;
    private MqttClient mqttClient;

    public MqttConsumer(MqttClient mqttClient, OrderRepository orderRepository) {
        System.out.println(mqttClient);
        this.mqttClient = mqttClient;
        this.orderRepository = orderRepository;
        System.out.println("-----------444-----------"+orderRepository);
    }



    @Override
    public void connectComplete(boolean reconnect, String serverURI) {
        log.debug("MqttConsumer:连接成功");
        try {
            //和EMQ连接成功后,订阅/topic/start/result 来消费充电桩发送的指令
            mqttClient.subscribe("/topic/start/result",0);
        } catch (MqttException e) {
            throw new RuntimeException(e);
        }

    }

    @Override
    public void connectionLost(Throwable cause) {
        log.debug("MqttConsumer:连接失败",cause);
    }

    @Override
    public void messageArrived(String topic, MqttMessage message) throws Exception {
        log.debug("MqttConsumer:消息到达:topic-{},message-{}", topic, message);
        DeviceResultParam deviceResultParam = JsonUtils.fromJson(message.toString(), DeviceResultParam.class);
        log.debug("MqttConsumer:消息转换完成:{}", deviceResultParam);
        if (deviceResultParam != null) {
            String result = deviceResultParam.getResult();
            if (Constant.RESULT_START_CHARGING_SUCCESS.equals(result)) {
                log.debug("充电桩开始充电");
                //创建订单成功记录
//                deviceResultParam.getOrderNo();
                log.debug("MqttConsumer:订单号:{}", deviceResultParam.getOrderNo());
//                orderService.saveSuccessOrder(deviceResultParam);
                ChargingBillSuccessPO chargingBillSuccessPO = new ChargingBillSuccessPO();
                System.out.println("==========111========="+deviceResultParam.getOrderNo());
                chargingBillSuccessPO.setBillId(deviceResultParam.getOrderNo());
                System.out.println("-----333-------------------");
                orderRepository.saveSuccessOrder(chargingBillSuccessPO);
                System.out.println("-----2222--------------------------");
            }else if (Constant.RESULT_START_CHARGING_FAIL.equals(result)){
                log.debug("充电桩故障");
                //创建订单失败记录
//                orderService.saveFailOrder();
                ChargingBillFailPO chargingBillFailPO = new ChargingBillFailPO();
                chargingBillFailPO.setBillId(deviceResultParam.getOrderNo());
                chargingBillFailPO.setFailDesc("设备自检失败");
                orderRepository.saveFailOrder(chargingBillFailPO);
            }
        }
    }

    @Override
    public void deliveryComplete(IMqttDeliveryToken token) {
        log.debug("MqttConsumer:消息处理完成");
    }
}

